package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> list(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        List<User> lists =userMapper.selectList(userDto);
        Page<User> page = (Page<User>) lists;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        String s = user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(s.getBytes()));
        userMapper.add(user);
    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.deleteById(ids);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername() +"123").getBytes()));
        userMapper.update(user);
    }

    @Override
    public User getById(Integer id) {
        User user = userMapper.getById(id);
        return user;
    }

    @Override
    public List<User> getAllList() {
        List<User> userList = userMapper.getAllList();
        return userList;
    }

    @Override
    public List<User> getByRoleLabel(String roleLabel) {
        List<User> userList = userMapper.getByRole(roleLabel);
        return userList;
    }

    @Override
    public List<User> getByDeptId(Integer deptId) {
        List<User> userList = userMapper.getByDeptId(deptId);
        return userList;
    }

    @Override
    public LoginResultVo login(User user) {
       User u = userMapper.getByUsername(user.getUsername());
       if (u == null){
           throw new BusinessException("用户名或密码错误");
       }
        String s = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!u.getPassword().equals(s)){
            throw new BusinessException("用户名或密码错误");
        }
        if (u.getStatus() != 1){
            throw new BusinessException("账号已停用");
        }
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());

        Map<String, Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());

        String  token = JwtUtils.generateToken(claims);
        loginResultVo.setToken(token);
        return loginResultVo;
    }
}
